package com.soulgame.sms;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;

import com.soul.sdk.common.IApplicationListener;
import com.soul.sdk.utils.SGDebug;

/**
 * @author lch
 * @version Create time：2017-1-3
 * @类说明:
 */
public class ApplicationProxySms implements IApplicationListener {

	@Override
	public void onProxyAttachBaseContext(Application pApplication, Context base) {
		// SGDebug.print_v("*********ApplicationProxySms onProxyAttachBaseContext*********");
	}

	@Override
	public void onProxyCreate(Application pApplication) {

		SGDebug.print_v("*********ApplicationProxySms onProxyCreate*********");

		try {
			if (pApplication.getPackageName().equals(getCurProcessName(pApplication))) {

				// Migu
				SmsSdkHelper.initMigu(pApplication);

				// MM
				SmsSdkHelper.initMM(pApplication);

			}
		} catch (Exception e) {
			SGDebug.print_e("onProxyCreate exception: " + e.getMessage());
			e.printStackTrace();
		}

	}

	@Override
	public void onProxyConfigurationChanged(Application pApplication, Configuration newConfig) {

		// SGDebug.print_v("*********ApplicationProxySms onProxyConfigurationChanged*********");

	}

	@Override
	public void onProxyTerminate(Application pApplication) {

		// SGDebug.print_v("*********ApplicationProxySms onProxyTerminate*********");

	}

	@Override
	public void onProxyLowMemory(Application pApplication) {

		// SGDebug.print_v("*********ApplicationProxySms onProxyLowMemory*********");

	}

	/**
	 * 获取当前进程名称
	 * 
	 * @param context
	 * @return
	 */
	private String getCurProcessName(Context context) {
		try {
			int pid = android.os.Process.myPid();
			ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
			for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
				if (appProcess.pid == pid) {
					SGDebug.print_d("processName=" + appProcess.processName);
					return appProcess.processName;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			SGDebug.print_e("getCurProcessName exception: " + e.getMessage());
		}
		return "";
	}

}
